package com.android.bandroid.api.util;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.lang.reflect.Type;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonDeserializationContext;
import com.google.gson.JsonDeserializer;
import com.google.gson.JsonElement;
import com.google.gson.JsonParseException;
import com.google.gson.JsonPrimitive;
import com.google.gson.JsonSerializationContext;
import com.google.gson.JsonSerializer;
import com.google.gson.reflect.TypeToken;

/**
 * json工具类
 *
 * @author Kevin
 */
public class JsonUtil {
    private static Gson gson = null;

    static {
        synchronized (JsonUtil.class) {
            if (gson == null) {
                gson = new Gson();
            }
        }
    }

    /**
     * 对象转json
     *
     * @param obj
     * @return
     */
    public static String toJson(Object obj) {
        String jsonString = null;
        if (gson != null) {
            jsonString = gson.toJson(obj);
        }
        return jsonString;
    }

    /**
     * 对象转json（自定义日期格式）
     *
     * @param obj
     * @param dateFormat
     * @return
     */
    public static String toJsonDateSerializer(Object obj,
                                              final String dateFormat) {
        String jsonString = null;
        gson = new GsonBuilder()
                .registerTypeHierarchyAdapter(Date.class,
                        new JsonSerializer<Date>() {
                            public JsonElement serialize(Date src,
                                                         Type typeOfSrc,
                                                         JsonSerializationContext context) {
                                SimpleDateFormat format = new SimpleDateFormat(
                                        dateFormat);
                                return new JsonPrimitive(format.format(src));
                            }
                        }).setDateFormat(dateFormat).create();
        if (gson != null) {
            jsonString = gson.toJson(obj);
        }
        return jsonString;
    }

    /**
     * json转为list
     *
     * @param jsonString
     * @return
     */
    public static List<?> toList(String jsonString) {
        List<?> objList = null;
        if (gson != null) {
            Type type = new TypeToken<List<?>>() {
            }.getType();
            objList = gson.fromJson(jsonString, type);
        }
        return objList;
    }

    /**
     * json转为bean
     *
     * @param jsString
     * @param clazz
     * @return
     */
    public static <T> T toBean(String jsonString, Class<?> clazz) {
        T obj = null;
        if (gson != null) {
            obj = (T) gson.fromJson(jsonString, clazz);
        }
        return obj;
    }

    /**
     * json转bean（自定义日期类型）
     *
     * @param jsonString
     * @param clazz
     * @param dateFormat
     * @return
     */
    public static <T> T toBeanDateSerializer(String jsonString, Class<?> clazz, final String dateFormat) {
        Object obj = null;
        gson = new GsonBuilder()
                .registerTypeAdapter(Date.class, new JsonDeserializer<Date>() {
                    public Date deserialize(JsonElement json, Type typeOfT,
                                            JsonDeserializationContext context)
                            throws JsonParseException {
                        Date result = null;
                        Calendar calendar = Calendar.getInstance();
                        String date = json.getAsJsonPrimitive().getAsString();
                        if (null != date) {
                            calendar.setTimeInMillis(Long.valueOf(date));
                            result = calendar.getTime();
                        }
                        return result;
                    }
                }).setDateFormat(dateFormat).create();
        if (gson != null) {
            obj = gson.fromJson(jsonString, clazz);
        }
        return (T) obj;
    }
}
